package com.example.myprojectoffeixue;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

import org.apache.http.entity.StringEntity;

import java.io.UnsupportedEncodingException;

public class SheZhifg extends Fragment {
    //设置fragment
    int versionCode;
    private float mDensity;
    private int mHiddenViewMeasuredHeight;
    private int mHiddenViewMeasuredHeight1;
    private String k;
    private LinearLayout l3;
    private LinearLayout l6;
    private MyApplication m;
    PackageInfo pi;
    boolean flag;
    int type;
    LinearLayout mHiddenLayout;
    LinearLayout mHiddenLayout2;
    LinearLayout l1_permissions;
    ImageView select_pression;
    ImageView select_baojing;
    LinearLayout l2_permissions;
    LinearLayout l3_permissions;
    LinearLayout l4_permissions;
    LinearLayout menci;
    LinearLayout mensuo;
    LinearLayout hongwai;
    TextView tv;
    private SharedPreferences sp;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        flag = true;
        View r = inflater.inflate(R.layout.shezhifg, container, false);// 关联布局文件
        LinearLayout l1 = (LinearLayout) r.findViewById(R.id.l1_shezhifg);
        l1_permissions = (LinearLayout) r.findViewById(R.id.l1_permissions);
        //调出菜单
        mHiddenLayout = (LinearLayout) r.findViewById(R.id.mHiddenLayout);
        mHiddenLayout2 = (LinearLayout) r.findViewById(R.id.mHiddenLayout2);
        l2_permissions = (LinearLayout) r.findViewById(R.id.l2_permissions);//授权一览
        l3_permissions = (LinearLayout) r.findViewById(R.id.l3_permissions);//分享网关
        l4_permissions = (LinearLayout) r.findViewById(R.id.l4_permissions);//分享设备
        l6 = (LinearLayout) r.findViewById(R.id.l6_shezhifg);//网关设置
        l6.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent  it=new Intent(getActivity(),GateWay_Shezhi.class);
                startActivity(it);
            }
        });
        l4_permissions.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent it=new Intent(getActivity(),ShareDeviceActivity.class);
                getActivity().startActivity(it);
            }
        });
        menci = (LinearLayout) r.findViewById(R.id.menci);
        mensuo = (LinearLayout) r.findViewById(R.id.mensuo);

        hongwai = (LinearLayout) r.findViewById(R.id.hongwai);
        menci.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent it = new Intent(getActivity(), BaoJingActivity.class);
                it.putExtra("flag", "menci");
                startActivity(it);

            }
        });

        mensuo.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent it = new Intent(getActivity(), BaoJingActivity.class);
                it.putExtra("flag", "mensuo");
                startActivity(it);

            }
        });

        hongwai.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent it = new Intent(getActivity(), BaoJingActivity.class);
                it.putExtra("flag", "hongwai");
                startActivity(it);
            }
        });

        l2_permissions.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent it = new Intent(getActivity(), PermissionsActivity.class);
                startActivity(it);
            }
        });


        l3_permissions.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent it = new Intent(getActivity(), Give_PermissionsActivity.class);
                startActivity(it);
            }
        });
        l3 = (LinearLayout) r.findViewById(R.id.l3_shezhifg);
        select_pression = (ImageView) r.findViewById(R.id.select_pression);
        select_baojing = (ImageView) r.findViewById(R.id.select_baojing);
        LinearLayout l4 = (LinearLayout) r.findViewById(R.id.l4_shezhifg);
        Button l5 = (Button) r.findViewById(R.id.l4tuichu);
        m = (MyApplication) getActivity().getApplication();
        tv = (TextView) r.findViewById(R.id.vison_shezhi);
        mDensity = getResources().getDisplayMetrics().density;
        mHiddenViewMeasuredHeight1 = (int) (mDensity * 150 + 0.5);
        mHiddenViewMeasuredHeight = (int) (mDensity * 150 + 0.5);
        l1_permissions.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                if (mHiddenLayout.getVisibility() == View.GONE) {
                    animateOpen(mHiddenLayout,1);
                    animationIvOpen(select_pression);
                } else {
                    animateClose(mHiddenLayout);
                    animationIvClose(select_pression);
                }
            }
        });
        PackageManager pm = getActivity().getPackageManager();
        try {
            pi = pm.getPackageInfo(getActivity().getPackageName(), PackageManager.GET_CONFIGURATIONS);
            tv.setText(pi.versionName);
        } catch (Exception e) {

        }
        l5.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                sp = getActivity().getSharedPreferences("first", getActivity().MODE_PRIVATE);
                sp.edit().putString("Mac", null).commit();
                sp.edit().putString("UserId", null).commit();
                sp.edit().putString("SocketToken", null).commit();
                sp.edit().putBoolean("isexit", true).commit();
                m.userId = null;
                Intent logoutIntent = new Intent(getActivity(), LoginActivity.class);
                logoutIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(logoutIntent);
                getActivity().finish();
            }
        });


        l1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent it = new Intent(getActivity(), XiuGaiMMActivity.class);
                startActivity(it);
            }
        });


//-----------------------------------------

        l3.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                if (mHiddenLayout2.getVisibility() == View.GONE) {
                    animateOpen(mHiddenLayout2,0);
                    animationIvOpen(select_baojing);
                } else {
                    animateClose(mHiddenLayout2);
                    animationIvClose(select_baojing);
                }
            }

        });

        l4.setOnClickListener(new OnClickListener() {

            private HttpUtils httpUtils;
            @Override
            public void onClick(View v) {
                versionCode = pi.versionCode;
                String os = "Android";
                Websocketutil ws = new Websocketutil(os, versionCode);
                String j = JSON.toJSONString(ws);
                j = AESUtil.encrypt(j);
                httpUtils = new HttpUtils();
                RequestParams params = new RequestParams();
                params.setHeader("Content-Type", "application/json");
                try {
                    params.setBodyEntity(new StringEntity(j, "utf-8"));
                } catch (UnsupportedEncodingException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                httpUtils.send(HttpMethod.POST, "http://" + UserClass.ServerIP + "/FXMobiServer/user/updateApp", params,
                        new RequestCallBack<String>() {

                            @Override
                            public void onFailure(HttpException arg0, String arg1) {
                                HttpConnection.showToast(getActivity(),"网络异常");
                            }
                            @Override
                            public void onSuccess(ResponseInfo<String> arg0) {
                                if (arg0.result != null && !("".equals(arg0.result))){

                                arg0.result = AESUtil.decrypt(arg0.result);
                                JSONObject j = JSONObject.parseObject(arg0.result);
                                JSONObject j1 = j.getJSONObject("latestVersion");
                                if (j1 != null) {
                                    k = j1.getString("url");

                                } else {

                                    HttpConnection.showToast(getActivity(),"版本" + pi.versionName + ",已经是最新版本");


                                }

                            }else{

                                    HttpConnection.showToast(getActivity(),"服务器错误");

                            }}

                        });
                if (k != null) {
                    UpdateApp u = new UpdateApp(getActivity(), k);
                    u.checkUpdateInfo();
                }

            }
        });


        return r;
    }


    private void animateOpen(View v,int type) {
        if(type==0){
        v.setVisibility(View.VISIBLE);
        ValueAnimator animator = createDropAnimator(v, 0, mHiddenViewMeasuredHeight);
        animator.start();}else if(type==1){
            v.setVisibility(View.VISIBLE);
            ValueAnimator animator = createDropAnimator(v, 0, mHiddenViewMeasuredHeight1);
            animator.start();

        }

    }
    private void animationIvOpen(ImageView iv) {
        RotateAnimation animation = new RotateAnimation(0, 90,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
                0.5f);
        animation.setFillAfter(true);
        animation.setDuration(100);
        iv.startAnimation(animation);
    }

    private void animationIvClose(ImageView iv) {
        RotateAnimation animation = new RotateAnimation(90, 0, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        animation.setFillAfter(true);
        animation.setDuration(100);
        iv.startAnimation(animation);
    }

    private void animateClose(final View view) {
        int origHeight = view.getHeight();
        ValueAnimator animator = createDropAnimator(view, origHeight, 0);
        animator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                view.setVisibility(View.GONE);
            }
        });
        animator.start();
    }

    private ValueAnimator createDropAnimator(final View v, int start, int end) {
        ValueAnimator animator = ValueAnimator.ofInt(start, end);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

            @Override
            public void onAnimationUpdate(ValueAnimator arg0) {
                int value = (int) arg0.getAnimatedValue();
                ViewGroup.LayoutParams layoutParams = v.getLayoutParams();
                layoutParams.height = value;
                v.setLayoutParams(layoutParams);
            }
        });
        return animator;
    }



























}
